import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { loadEnv } from 'vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default ({ command, mode }) => {
    return defineConfig({
        plugins: [
            vue(),
            createSvgIconsPlugin({
                // 指定需要缓存的图标文件夹

                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],

                // 指定symbolId格式

                symbolId: '[dir]-[name]'
            })
        ],
        server: {
            host: '0.0.0.0',
            port: Number(loadEnv(mode, process.cwd()).VITE_APP_PORT),
            strictPort: true,
            https: false,
            open: loadEnv(mode, process.cwd()).VITE_APP_OPEN == 'true',
            // proxy: {
            //     '/api': {
            //         target: loadEnv(mode, process.cwd()).VITE_APP_SERVER,
            //         changeOrigin: true,
            //         rewrite: (path) => path.replace(/^\/api/, '')
            //     }
            // }
            proxy: {
                // 把key的路径代理到target位置
                // detail: https://cli.vuejs.org/config/#devserver-proxy
                '/api': {
                    // 需要代理的路径   例如 '/api'
                    target: `${loadEnv(mode, process.cwd()).VITE_BASE_SERVER}/`, // 代理到 目标路径
                    changeOrigin: true,
                    rewrite: (path) =>
                        path.replace(
                            new RegExp(
                                '^' + loadEnv(mode, process.cwd()).VITE_BASE_API
                            ),
                            ''
                        )
                }
            }
        },
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src')
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@import "@/assets/css/global.scss";'
                }
            }
        },
        build: {
            chunkSizeWarningLimit: 1500,
            rollupOptions: {
                output: {
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id
                                .toString()
                                .split('node_modules/')[1]
                                .split('/')[0]
                                .toString()
                        }
                    }
                }
            }
        }
    })
}
